const path = require('path');
const bodyParser = require('body-parser');
const webpack = require('webpack');
const mockMiddleware = require('./config/mock.config.js');
const envConfig = require('./config/env');

const cookie = '';

module.exports = {
    babel: {},
    eslint: {
        mode: 'file',
    },
    webpack: {
        alias: {
            ['@']: path.resolve(__dirname, 'src')
        },
        plugins: {
            add: [
                new webpack.DefinePlugin({
                    'process.env.APP_ENV': JSON.stringify(process.env.APP_ENV),
                    'process.env.ENV_CONFIG': JSON.stringify(envConfig)
                })
            ]
        },
        configure: (webpackConfig, { env, paths }) => {
            if (env === 'production') {
                webpackConfig.output.filename = 'static/js/[name].js';
                webpackConfig.output.chunkFilename = 'static/js/[name].chunk.js';
                webpackConfig.output.assetModuleFilename = 'static/media/[name][ext]';
                // 配置生成环境下的路径
                webpackConfig.output.publicPath = '/screen/business/';
            }

            return webpackConfig;
        }
    },
    devServer: (devServerConfig, { env, paths, proxy, allowedHost }) => {
        devServerConfig.onAfterSetupMiddleware = function(devServer) {
            const app = devServer.app;
            app.use(bodyParser.json());
            app.use(mockMiddleware);
        };
        // 使用proxy配置本地联调转发，解决跨域问题
        // const onProxyReq = proxyReq => {
        //     proxyReq.setHeader('cookie', cookie);
        //     if (proxyReq.getHeader('origin')) {
        //         proxyReq.setHeader('origin', 'http://39.106.224.106:8090');
        //     }
        // };
        // devServerConfig.proxy = [
        //     {
        //         context: ['/finance'],
        //         target: 'http://39.106.224.106:8090/',
        //         changeOrigin: true,
        //         secure: false,
        //         onProxyReq
        //     }
        // ];
        return devServerConfig;
    },
    plugins: [
        {
            plugin: {
                overrideWebpackConfig: ({
                    webpackConfig,
                    cracoConfig,
                    pluginOptions,
                    context: { env, paths }
                }) => {
                    if (env === 'production') {
                        const { getPlugin, pluginByName } = require("@craco/craco");

                        const { isFound, match } = getPlugin(webpackConfig, pluginByName("MiniCssExtractPlugin"));

                        if (isFound && match?.options) {
                            match.options.filename = 'static/css/[name].css';
                            match.options.chunkFilename = 'static/css/[name].chunk.css';
                            match.options.ignoreOrder = true;
                        }
                    }
                    return webpackConfig;
                },
            },
        }
    ]
};
